﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FloodFree.Common;
using FloodFree.Web.ViewModels;

namespace FloodFree.Web.Controllers.Cities
{
    public class CitiesController : BaseController
    {
        private FloodFreeContext db = new FloodFreeContext();

        [HttpGet]
        public ActionResult Show(int id)
        {

            String username  = HttpContext.User.Identity.Name;
            List<Subscription> subs = null;
            CityShowViewModel vm = new CityShowViewModel();

            City city = db.Cities
                .Include("Sensors")
                .Include("Sensors.Updates")
                .Where(c => c.Id == id)
                .FirstOrDefault();

           


            if (HttpContext.User.Identity.IsAuthenticated)
            {
                subs = db.Subscriptions
                    .Where(x => x.Username == username)
                    .ToList();

                vm.Subscriptions = subs;
            }

            if (city == null)
                return RedirectToAction("NotFound");

            
            vm.City = city;
            return View(vm);
        }

        public ActionResult NotFound()
        {
            return View();
        }

        

    }
}
